﻿using WebApp.BLL.Base;
using WebApp.Common;
using WebApp.Common.Caching;
using WebApp.DAL;
using WebApp.Models;
using WebApp.ViewModels;
using WebApp.ViewModels.Manage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApp.BLL;
using System.Web.Security;
using WebApp.BLL.Basic;
using System.Web.Mvc;

namespace WebApp.BLL.Manage
{
    public class S_departmentService : BaseService<S_department>
    {
        #region const


        #endregion

        #region DAL

        #endregion

        #region 构造

        public S_departmentService() : base(new S_departmentRepository()) { }

        #endregion

        #region FindByDeptname

        public S_department FindByDeptname(string deptname)
        {
            return this.CurrentRepository.Find(p => p.Deptname == deptname && p.Isdeleted == 0);
        }

        #endregion

        #region ListByDeptType

        public List<S_department> ListByDeptType(string depttype)
        {
            return this.CurrentRepository.Entities.Where(p => p.Isdeleted == 0 && p.Depttype == depttype).OrderBy(p => p.Sort).ToList();
        }

        #endregion

        #region Exist

        public bool Exist(int id, string depttype, string deptname)
        {
            if (id == 0)
            {
                return this.CurrentRepository.Entities.FirstOrDefault(p => p.Isdeleted == 0 && p.Depttype == depttype && p.Deptname == deptname) != null;
            }
            else
            {
                return this.CurrentRepository.Entities.FirstOrDefault(p => p.Id != id && p.Isdeleted == 0 && p.Depttype == depttype && p.Deptname == deptname) != null;
            }
        }

        #endregion

        #region GetName

        /// <summary>
        /// 根据用户ID查询姓名
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public string GetName(int? deptid)
        {
            if (!deptid.HasValue) return string.Empty;

            var item = this.CurrentRepository.Entities.FirstOrDefault(p => p.Id == deptid.Value);
            if (item == null)
            {
                return string.Empty;
            }
            else
            {
                return item.Deptname;
            }
        }

        #endregion

        #region PageList

        public List<S_department> PageList(int pageindex, int pagesize, string depttype, string searchkey, string sort, string order, out int rowscount)
        {
            var T = this.CurrentRepository.Entities.Where(p => p.Isdeleted == 0);

            //类型
            T = T.Where(p => p.Depttype == depttype);

            //根据属性名实现动态排序
            if (string.IsNullOrEmpty(sort)) sort = "Id";
            T = base.SortList(T, sort, order == "asc" ? true : false);

            //查询条件
            if (!string.IsNullOrEmpty(searchkey))
            {
                T = T.Where(p => p.Deptname.Contains(searchkey));
            }

            //返回记录数
            rowscount = T.Count();
            //返回分页结果
            return this.CurrentRepository.PageList(T, pageindex, pagesize).ToList();
        }

        #endregion

        #region Save

        public bool Save(S_department item)
        {
            if (item.Id == 0)
            {
                this.CurrentRepository.Add(item);
            }
            else
            {
                this.CurrentRepository.Update(item);
            }

            return true;
        }

        #endregion

        #region CacheList

        /// <summary>
        /// 从缓存获取所有S_dict
        /// </summary>
        /// <returns></returns>
        public List<S_department> CacheList()
        {
            var cachekey = "S_department";

            var result = new MemoryCacheManager().Get<List<S_department>>(cachekey, 60, () =>
            {
                return this.CurrentRepository.Entities
                    .OrderBy(p => p.Deptname)
                    .ToList();
            });

            return result;
        }

        /// <summary>
        /// 更新数据后，刷新缓存
        /// </summary>
        public void RefreshCache()
        {
            var cachekey = "S_department";

            var list = this.CurrentRepository.Entities
                    .OrderBy(p => p.Deptname)
                    .ToList();

            new MemoryCacheManager().Remove(cachekey);
            new MemoryCacheManager().Set(cachekey, list, 60);
        }

        #endregion

        #region GetSelectList

        public SelectList GetSelectList(IEnumerable<int> ids, string selectedValue = "")
        {
            var T = this.CurrentRepository.Entities.Where(p => ids.Contains(p.Id) && p.Isdeleted == 0);

            var list = T.OrderBy(p => p.Sort).ThenBy(p => p.Deptname).Select(p => new
            {
                value = p.Id,
                text = p.Deptname
            }).ToList();

            return new SelectList(list, "value", "text", selectedValue);
        }

        public SelectList GetSelectList(string depttype, string selectedValue = "")
        {
            var T = this.CurrentRepository.Entities.Where(p => p.Depttype == depttype && p.Isdeleted == 0);

            var list = T.OrderBy(p => p.Sort).ThenBy(p => p.Deptname).Select(p => new
            {
                value = p.Id,
                text = p.Deptname
            }).ToList();

            return new SelectList(list, "value", "text", selectedValue);
        }

        #endregion

        #region 删除

        public bool DeleteByIds(string ids)
        {
            foreach (string id in ids.Split(','))
            {
                if (string.IsNullOrEmpty(id)) continue;

                //逻辑删除
                var item = this.CurrentRepository.Find(Convert.ToInt32(id));
                item.Isdeleted = 1;
                this.CurrentRepository.Update(item, false);
            }

            this.CurrentRepository.Save();

            return true;
        }

        #endregion

    }

}
